<?php defined('BASEPATH') OR exit('No direct script access allowed');

class User_model extends WS_model
{
    public function validateLogin($user_email, $password)
    {
        $this->db->from('user u');
        $this->db->join('user_status us', 'us.user_status_seq_id = u.user_status_seq_id', 'left');
        $this->db->join('user_type ut', 'ut.user_type_seq_id = u.user_type_seq_id', 'left');
        $this->db->where('user_email', $user_email);
        $this->db->where('user_password', ws_password_hash($password));
        $query = $this->db->get();
        if ($query->num_rows())
            return $query->row();
        return false;
    }

    public function validateLoginBYID($user_id, $password)
    {
        $this->db->from('user u');
        $this->db->join('user_status us', 'us.user_status_seq_id = u.user_status_seq_id', 'left');
        $this->db->join('user_type ut', 'ut.user_type_seq_id = u.user_type_seq_id', 'left');
        $this->db->where('user_seq_id', $user_id);
        $this->db->where('user_password', $password);
        $query = $this->db->get();
        if ($query->num_rows())
            return $query->row();
        return false;
    }

    public function get_userById($id)
    {
        $this->db->from('user u');
        $this->db->join('user_status us', 'us.user_status_seq_id = u.user_status_seq_id', 'left');
        $this->db->join('user_type ut', 'ut.user_type_seq_id = u.user_type_seq_id', 'left');
        $this->db->where('user_seq_id', $id);

        $query = $this->db->get();
        if ($query->num_rows())
            return $query->row();
        return false;
    }

    public function get_userByEmail($email)
    {
        $this->db->from('user u');
        $this->db->join('user_status us', 'us.user_status_seq_id = u.user_status_seq_id', 'left');
        $this->db->join('user_type ut', 'ut.user_type_seq_id = u.user_type_seq_id', 'left');
        $this->db->where('user_email', $email);

        return $this->row();
    }

    public function users_count($filter)
    {
        $this->db->from('user u');
        $this->db->join('user_status us', 'us.user_status_seq_id = u.user_status_seq_id', 'left');
        $this->db->join('user_type ut', 'ut.user_type_seq_id = u.user_type_seq_id', 'left');

        if (isset($filter['status']))
            $this->db->where('u.user_status_seq_id', $filter['status']);
        if (isset($filter['access_type']))
            $this->db->where('u.user_type_seq_id', $filter['access_type']);
        if (isset($filter['name'])) {
            $where = " (u.user_email LIKE '%{$filter['name']}%' OR u.user_name LIKE '%{$filter['name']}%')";
            $this->db->where($where, NULL, false);
        }

        return $this->db->count_all_results();
    }

    public function get_all_users($filter)
    {
        $this->db->from('user u');
        $this->db->join('user_status us', 'us.user_status_seq_id = u.user_status_seq_id', 'left');
        $this->db->join('user_type ut', 'ut.user_type_seq_id = u.user_type_seq_id', 'left');

        if (isset($filter['status']))
            $this->db->where('u.user_status_seq_id', $filter['status']);
        if (isset($filter['access_type']))
            $this->db->where('u.user_type_seq_id', $filter['access_type']);
        if (isset($filter['name'])) {
            $where = " (u.user_email LIKE '%{$filter['name']}%' OR u.user_name LIKE '%{$filter['name']}%')";
            $this->db->where($where, NULL, false);
        }
        if (isset($filter['order_by']) && isset($filter['order_type']))
            $this->db->order_by($filter['order_by'], $filter['order_type']);
        else  $this->db->order_by('user_seq_id', 'desc');
        $this->db->limit($filter['per_page'], $filter['offset']);
        $query = $this->db->get();
        if ($query->num_rows())
            return $query->result();
        return false;
    }

    public function get_all_status($convert = true)
    {
        $this->db->from('user_status');
        $query = $this->db->get();
        if ($query->num_rows()) {
            if ($convert) {
                return $this->get_all_status_convert($query->result());
            } else {
                return $query->result();
            }
        }
        return false;
    }

    private function get_all_status_convert($data)
    {
        $return = array();
        foreach ($data as $d) {
            $return[$d->user_status_seq_id] = $d->user_status_name;
        }
        return $return;
    }

    public function get_all_user_type($convert = true)
    {
        $this->db->from('user_type');
        $query = $this->db->get();
        if ($query->num_rows()) {
            if ($convert) {
                return $this->get_all_user_type_convert($query->result());
            } else {
                return $query->result();
            }
        }
        return false;
    }

    private function get_all_user_type_convert($data)
    {
        $return = array();
        foreach ($data as $d) {
            $return[$d->user_type_seq_id] = $d->user_type_name;
        }
        return $return;
    }

    public function insert($data, $bulk = false)
    {
        if ($bulk) {
            $this->insert_batch('user', $data);
        } else {
            $this->db->insert('user', $data);
            return $this->db->insert_id();
        }
    }

    public function update_user($data, $where)
    {
        $this->db->update('user', $data, $where);
        return $this->db->affected_rows();
    }

    public function delete_userByID($id)
    {
        $this->db->delete('user', array('user_seq_id' => $id));
        return $this->db->affected_rows();
    }

    public function delete_bulkUsers($user_ids)
    {
        $this->db->where_in('user_seq_id', $user_ids);
        $this->db->delete('user');
        return $this->db->affected_rows();
    }
}
